package com.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.ArticleMapper;

import com.mapper.FocusMapper;
import com.mapper.SysUserMapper;
import com.mapper.UserInfoMapper;
import com.pojo.dto.Message;
import com.pojo.entity.Focus;
import com.pojo.vo.FocusVo;
import com.pojo.vo.SelectVo;
import com.service.FocusService;
import com.service.SysUtilService;
import com.util.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FocusServiceImpl implements FocusService {
    @Autowired
    FocusMapper focusMapper;
    @Autowired
    SysUserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    SysUtilService sysUtilService;
    @Autowired
    ArticleMapper articleMapper;
    @Resource(name = "jsonRedisTemplate")
    RedisTemplate redisTemplate;
    @Override
    public Message<List<FocusVo>> getFocusById(Long id) {
        List<FocusVo> focusVos=new ArrayList<>();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",id);
        List<Focus> list = focusMapper.selectList(wrapper);
        for (int i = 0; i <list.size(); i++) {
            FocusVo focusVo = PojoUtil.copyObject(list.get(i), FocusVo.class);
            focusVo.setFocusName(userMapper.selectById(list.get(i).getFocusId()).getUsername());
            focusVos.add(focusVo);
        }
        return Message.successMessage((long) focusVos.size(),focusVos);
    }

    @Override
    public Message<List<FocusVo>> getFansById(Long id) {
        List<FocusVo> fans=new ArrayList<>();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("focus_id",id);
        List<Focus> list = focusMapper.selectList(wrapper);
        for (int i = 0; i <list.size(); i++) {
            FocusVo focusVo = PojoUtil.copyObject(list.get(i), FocusVo.class);
            focusVo.setUserName(userMapper.selectById(list.get(i).getUserId()).getUsername());
            fans.add(focusVo);
        }
        return Message.successMessage((long) fans.size(),fans);
    }

    @Override
    public Message<List<FocusVo>> getStarts() {
       // List<FocusVo> focusVos = focusMapper.selectFocusCount();
        List<FocusVo> fansVo = focusMapper.selectFansCount();
        fansVo.forEach(f->{
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("user_id",f.getFocusId());
            QueryWrapper userWrapper=new QueryWrapper();
            Long focusId=f.getFocusId();
            userWrapper.eq("user_num",focusId);
            List list = focusMapper.selectList(wrapper);
            f.setFocusCount((long) list.size());
            f.setUserName(userMapper.selectById(focusId).getUsername());
            f.setTouXiang(userInfoMapper.selectOne(userWrapper).getPath());
            Long sex = userMapper.selectById(focusId).getSex();

            Long address=userInfoMapper.selectOne(userWrapper).getCity();
            f.setSex(getText(sex,"user_sex"));
            f.setAddress(getText(address,"syscity"));
        });
        return Message.successMessage((long) fansVo.size(),fansVo);
    }


    @Override
    public Message<String> focusFriend(Focus focus) {
        int insert = focusMapper.insert(focus);
        if(insert>0)return  Message.successMessage((long) insert,"关注成功");
        else  return Message.errorMessage("0","关注好友失败");
    }

    @Override
    public Message<String> deleteFocus(Focus focus) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",focus.getUserId());
        wrapper.eq("focus_id",focus.getFocusId());
        int delete = focusMapper.delete(wrapper);
        if(delete>0)return  Message.successMessage((long) delete,"取消关注成功");
        else  return Message.errorMessage("0","取消关注失败");
    }

    public String getText(Long id,String name){
        SelectVo selectone = sysUtilService.selectone(id, name);
        return selectone.getText();
    }

    public Message<FocusVo> getDiaoYou(Long id,Long userId){
        FocusVo f=new FocusVo();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",id);
        QueryWrapper userWrapper=new QueryWrapper();
        userWrapper.eq("user_num",id);
        List list = focusMapper.selectList(wrapper);
        f.setUserId(id);
        f.setFocusCount((long) list.size());
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("focus_id",id);
        List list1 = focusMapper.selectList(wrapper1);
        f.setFansCount((long) list1.size());
        f.setUserName(userMapper.selectById(id).getUsername());
        f.setTouXiang(userInfoMapper.selectOne(userWrapper).getPath());
        Long sex = userMapper.selectById(id).getSex();
        Long address=userInfoMapper.selectOne(userWrapper).getCity();
        f.setSex(getText(sex,"user_sex"));
        f.setAddress(getText(address,"syscity"));
        f.setGeQian("夏钓鱼追饵，冬钓饵追鱼");
        QueryWrapper wrapper2=new QueryWrapper();
        wrapper2.eq("id",id);
        List list2 = articleMapper.selectList(wrapper2);
        f.setArtCount((long) list2.size());
        QueryWrapper wrapper3=new QueryWrapper();
        wrapper3.eq("user_id",userId);
        wrapper3.eq("focus_id",id);
        Focus focus = focusMapper.selectOne(wrapper3);
        if(focus==null){
            f.setFocused(false);
        }else f.setFocused(true);
        return  Message.successMessage((long) 1,f);

    }
}
